<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="left">
      <el-form-item label="openId" prop="openId">
        <el-input type="text" v-model="form.openId" placeholder="openId" clearable></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input type="text" v-model="form.nickName" placeholder="昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <sc-select v-model="form.sex" dic="sex" placeholder="请选择性别" clearable filterable ></sc-select>
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input type="text" v-model="form.city" placeholder="城市" clearable></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="headImgUrl">
        <el-input type="text" v-model="form.headImgUrl" placeholder="头像" clearable></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="tagList">
        <el-input type="text" v-model="form.tagList" placeholder="标签" clearable></el-input>
      </el-form-item>
      <el-form-item label="订阅时间" prop="subscribeTime">
        <el-date-picker v-model="form.subscribeTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择订阅时间" ></el-date-picker>
      </el-form-item>
      <el-form-item label="场景值" prop="subscribeScene">
        <el-input type="text" v-model="form.subscribeScene" placeholder="场景值" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否关注" prop="subscribe">
        <el-select v-model="form.subscribe" placeholder="请选择是否关注" clearable >
          <el-option value="0">请自行添加选项</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaving: false,
      //表单数据
      form: {
        openId: "",
        nickName: "",
        sex: "",
        city: "",
        headImgUrl: "",
        tagList: "",
        subscribeTime: "",
        subscribeScene: "",
        subscribe: "",
      },
      //验证规则
      rules: {
        openId: [
          {required: true, message: '请输入openId'}
        ],
        nickName: [
          {required: true, message: '请输入昵称'}
        ],
        sex: [
          {required: true, message: '请输入性别'}
        ],
        city: [
          {required: true, message: '请输入城市'}
        ],
        headImgUrl: [
          {required: true, message: '请输入头像'}
        ],
        tagList: [
          {required: true, message: '请输入标签'}
        ],
        subscribeTime: [
          {required: true, message: '请输入订阅时间'}
        ],
        subscribeScene: [
          {required: true, message: '请输入场景值'}
        ],
        subscribe: [
          {required: true, message: '请输入是否关注'}
        ],
      },
    }
  },
  mounted() {
  },
  methods: {
    //显示
    open(mode='add'){
      this.mode = mode;
      this.visible = true;
      return this
    },
    //表单提交方法
    submit(){
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaving = true;
          let res;
          if(this.mode === 'edit')res = await this.$API.wechat.wechatOfficialAccountFans.edit.post(this.form)
          else if(this.mode === 'add')res = await this.$API.wechat.wechatOfficialAccountFans.save.post(this.form)
          this.isSaving = false;
          if(res.success){
            this.$emit('success', this.form, this.mode)
            this.visible = false;
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData(data){
      Object.assign(this.form, data)
    }
  }
}
</script>

<style>
</style>